package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.widget.CheckBox;
import android.widget.DialerFilter;
import android.widget.EditText;
import android.widget.Toast;

public class PhoneBank extends Activity implements OnClickListener {
	private static final String TAG = null;
	WebServiceClient mWebServiceClient;
	protected Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_phone_bank);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		findViewById(R.id.yinhangkazhuanzhang).setOnClickListener(this);
		findViewById(R.id.yuebianxian).setOnClickListener(this);
		findViewById(R.id.yuechaxun).setOnClickListener(this);
		findViewById(R.id.zhuanzhanglishi).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yinhangkazhuanzhang: {
			AlertDialog.Builder builder = new Builder(PhoneBank.this);
			builder.setTitle("物银卡转账");
			LayoutInflater inflater = LayoutInflater.from(PhoneBank.this);
			View view = inflater
					.inflate(R.layout.dlg_yinhangkazhuanzhang, null);
			final EditText money = (EditText) view.findViewById(R.id.money);
			final CheckBox withdrawFromRealBankCard = (CheckBox) view
					.findViewById(R.id.withdrawFromRealBankCard);
			builder.setView(view);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							final String m = money.getText().toString();
							final boolean w = withdrawFromRealBankCard
									.isChecked();

							new AsyncTask<Void, Void, Object>() {

								protected void onPostExecute(Object result) {
									if (result instanceof Exception) {
										Exception e = (Exception) result;
										Toast.makeText(PhoneBank.this,
												"失败，请稍候再试", Toast.LENGTH_LONG)
												.show();
									} else {
										Toast.makeText(PhoneBank.this, "成功",
												Toast.LENGTH_LONG).show();
									}
								};

								@Override
								protected Object doInBackground(Void... params) {
									try {
										return mWebServiceClient
												.common_transferToSystemAccount(
														mCertificate, m, w);
									} catch (IOException e) {
										return e;
									} catch (XmlPullParserException e) {
										return e;
									}
								}
							}.execute();

						}
					});
			builder.setNegativeButton("取消", null);
			builder.show();
		}
			break;
		case R.id.yuebianxian: {
			AlertDialog.Builder builder = new Builder(PhoneBank.this);
			builder.setTitle("余额变现");
			LayoutInflater inflater = LayoutInflater.from(PhoneBank.this);
			View view = inflater.inflate(R.layout.dlg_yuebianxian, null);
			final EditText money = (EditText) view.findViewById(R.id.money);
			builder.setView(view);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							final String m = money.getText().toString();

							new AsyncTask<Void, Void, Object>() {

								protected void onPostExecute(Object result) {
									if (result instanceof Exception) {
										Exception e = (Exception) result;
										Toast.makeText(PhoneBank.this,
												"失败，请稍候再试", Toast.LENGTH_LONG)
												.show();
									} else {
										Toast.makeText(PhoneBank.this, "成功",
												Toast.LENGTH_LONG).show();
									}
								};

								@Override
								protected Object doInBackground(Void... params) {
									try {
										return mWebServiceClient
												.common_transferToUserAccount(
														mCertificate, m);
									} catch (IOException e) {
										return e;
									} catch (XmlPullParserException e) {
										return e;
									}
								}
							}.execute();

						}
					});
			builder.setNegativeButton("取消", null);
			builder.show();
		}
			break;
		case R.id.yuechaxun:
			new AsyncTask<Void, Void, Object>() {

				protected void onPostExecute(Object result) {
					if (result instanceof Exception) {
						Exception e = (Exception) result;
						Toast.makeText(PhoneBank.this, "刷新失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					} else {
						SoapPrimitive so = (SoapPrimitive) result;
						// mBalance.setText(String.format("我的物银卡（余额： %s RMB）",
						// so.toString()));

						AlertDialog.Builder bulder = new Builder(PhoneBank.this);
						bulder.setTitle("余额查询");
						bulder.setMessage("余额: " + so.toString());
						bulder.setNeutralButton("确定", null);
						bulder.show();
					}
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.common_getBalance(mCertificate);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();

			break;
		case R.id.zhuanzhanglishi:
			Intent i = new Intent(this, ZhuanZhangHistory.class);
			startActivity(i);
			break;
		}
	}
}
